package cn.highedu.spring;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;

import java.util.ArrayList;
@ComponentScan("cn.highedu.spring")
public class MyConfigTest {
    ApplicationContext context = null;
    @BeforeEach
    public void createContext() {
        context = new AnnotationConfigApplicationContext(MyConfigTest.class);
    }

    @Test
    public void test1() {
        // 只根据标识(id) 获取Spring 容器中被管理的对象, 得到的结果是Object类型, 需要通过强转成需要的数据类型
        ArrayList<String> names = (ArrayList<String>) context.getBean("names");
        names.forEach(System.out::println);
    }

    @Test
    public void test2() {
        ArrayList<String> cities = context.getBean("cities", ArrayList.class);
        cities.forEach(System.out::println);
    }

    @Test
    public void test3() {
        ArrayList<String> moreCities = context.getBean("moreCities", ArrayList.class);
        moreCities.forEach(System.out::println);
    }

    @Test
    public void test4() {
        User user = context.getBean("getUser", User.class);
        System.out.println(user);
    }
}
